package abc.red.controller;

import abc.red.entity.D;
import abc.red.entity.Goods;
import abc.red.entity.R;
import abc.red.service.GoodsService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * @author YiXia
 */
@Slf4j
@RestController
@RequestMapping("/goods")
public class GoodsController {


    @Autowired
    private GoodsService service;
    //默认每页数据量
    private final int pageSize = 10;

    /**
     * 设置分页查询的属性
     */
//    private int pageSize = 10;
//
    @PostMapping("/getOneById")
    public R<Goods> getOneById(@RequestBody D d) {

        Goods sqlS = service.getById(d.getId());

        //数据肯定存在，直接返回信息
        return R.success(sqlS);
    }
    @PostMapping("/getAllGoodsByPages")
    public R<IPage> getAllGoodsByPages(@RequestBody D d) {
        //超级管理员可以查询所有
        IPage<Goods> page = new Page<>(d.getNum1(), pageSize);
        service.page(page);
        return R.success(page);
    }

    //
//
//
//
    @PostMapping("/add")
    public R<String> add(@RequestBody Goods g) {
        boolean end = service.save(g);
        if (end) {
            log.info("打印 GoodsController-add 方法上传的 Goods 对象数据   {}", g.toString());
            return R.success("新增成功");
        } else {
            return R.error("新增失败");
        }
    }
//
//    @PostMapping("/changeStatus")
//    public R<String> changeStatus(@RequestBody D d) {
//        log.info("打印参数D "+d.toString());
//        Supplier sqlS = service.getById(d.getId());
//        sqlS.setStatus((sqlS.getStatus()+1)%2);
//        //防手贱代码，保护超级管理员账号
//
//        boolean end = service.updateById(sqlS);
//        log.info("打印SupplierController-changeStatus方法上传的Supplier对象数据   {}",sqlS.toString());
//        if (end) {
//            return R.success("修改成功");
//        } else {
//            return R.error("修改失败");
//        }
//    }
//
//
    @PostMapping("/deleteById")
    public R<String> deleteById(@RequestBody Goods g) {

        boolean end = service.removeById(g);
        if (end) {
            log.info("打印 GoodsController -deleteById方法上传的 Goods 对象数据   {}",g.toString());
            return R.success("删除成功");
        } else {
            return R.error("删除失败");
        }
    }
//
//
    @PostMapping("/editById")
    public R<String> editById(@RequestBody Goods g) {
        boolean end = service.updateById(g);
        if (end) {
            log.info("打印 GoodsController -editById方法上传的 Goods 对象数据   {}",g.toString());
            return R.success("编辑成功");
        } else {
            return R.error("编辑失败");
        }
    }




}
